﻿using Android.App;
using Android.Widget;
using HeWeatherAPI.Model;
using System.Collections.Generic;
using Weather;

namespace Weather.Adapter
{
    public class DailyAdapter : BaseAdapter<DailyForcast>
    {

        class ViewHolder : Java.Lang.Object
        {
            public TextView DateView { get; set; }
            public TextView MaxView { get; set; }
            public TextView MinView { get; set; }
            public TextView CondtionView { get; set; }

        }

        protected Activity context = null;
        protected IList<DailyForcast> dailys;

        public DailyAdapter(Activity context, IList<DailyForcast> dailys)
            : base()
        {
            this.context = context;
            this.dailys = dailys;
        }

        public override DailyForcast this[int position]
        {
            get { return dailys[position]; }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get
            {

                if (dailys.Count != 0)
                {
                    var headerDate = context.FindViewById<TextView>(Resource.Id.HeaderDate);
                    var headerMax = context.FindViewById<TextView>(Resource.Id.HeaderMax);
                    var headerMin = context.FindViewById<TextView>(Resource.Id.HeaderMin);
                    var headerCondition = context.FindViewById<TextView>(Resource.Id.HeaderCondition);
                    headerDate.Text = "Date";
                    headerMax.Text = "Temp.Max";
                    headerMin.Text = "Temp.Min";
                    headerCondition.Text = "Condition";
                }
                return dailys.Count;
            }
        }

        public override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
        {
            // Get our object for position
            var item = dailys[position];

            ViewHolder vh = null;
            if (convertView == null)
            {
                vh = new ViewHolder();
                convertView = context.LayoutInflater.Inflate(Resource.Layout.DailyItem, parent, false);
                vh.DateView = convertView.FindViewById<TextView>(Resource.Id.TxtDate);
                vh.MaxView = convertView.FindViewById<TextView>(Resource.Id.TxtMax);
                vh.MinView = convertView.FindViewById<TextView>(Resource.Id.TxtMin);
                vh.CondtionView = convertView.FindViewById<TextView>(Resource.Id.TxtCondition);
                convertView.Tag = vh;
            }
            else
            {
                vh = (ViewHolder)convertView.Tag;
            }

            vh.DateView.Text = item.Date.ToString("yyyy-MM-dd");
            vh.MaxView.Text = string.Format("{0}℃", item.MaxTemperature.ToString());
            vh.MinView.Text = string.Format("{0}℃", item.MinTemperature.ToString());
            vh.CondtionView.Text = item.Condition;
            //Finally return the view
            return convertView;
        }
    }
}